package com.suirich.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suirich.bean.JobReference;
import com.suirich.common.Constants;
import com.suirich.common.Page;
import com.suirich.service.JobReferenceService;

@Controller
@RequestMapping("/jobReference")
public class JobReferenceController {
	@Autowired
	private JobReferenceService jobReferenceService;

	@RequestMapping(value="/create",method=RequestMethod.POST)
    @ResponseBody
	public Map<String,String> create(JobReference jobReference){

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            Date nowDate = new Date();
            jobReference.setCreateDate(nowDate);
            jobReference.setUpdateDate(nowDate);
            
            this.jobReferenceService.create(jobReference);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }

        return result;
	}

    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(JobReference jobReference){

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            this.jobReferenceService.delete(jobReference);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        

        return result;
    }
    
    @RequestMapping(value="/list",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> list(Page page){

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
            List<JobReference> jobReferenceList = this.jobReferenceService.getJobReferenceList(page);
            Long jobReferenceListCount = this.jobReferenceService.getJobReferenceListCount();

            result.put("jobReferenceList", jobReferenceList);
            result.put("jobReferenceListCount", jobReferenceListCount);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        return result;
    }
    
    @RequestMapping(value="/update",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> update(JobReference jobReference){
        Map<String,String> result = new HashMap<String,String>();
        
        try {
            JobReference originalJobReference = this.jobReferenceService.read(jobReference.getId());
            if(originalJobReference != null){
                originalJobReference.setSubject(jobReference.getSubject());
                originalJobReference.setLightSoure(jobReference.getLightSoure());
                originalJobReference.setImagePath(jobReference.getImagePath());
            }
            
            this.jobReferenceService.update(originalJobReference);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        return result;
    }

    @RequestMapping(value="/top",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> top(JobReference jobReference){
        Map<String,String> result = new HashMap<String,String>();
        
        try {
            JobReference originalJobReference = this.jobReferenceService.read(jobReference.getId());
            if(originalJobReference != null){
                originalJobReference.setUpdateDate(new Date());
            }
            
            this.jobReferenceService.update(originalJobReference);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }

        return result;
    }
}
